/**
 * Created with IntelliJ IDEA.
 * User: Tonis
 * Date: 06.05.12
 * Time: 12:39
 * To change this template use File | Settings | File Templates.
 */
function MultiPlayer(id) {
    this.setID(id);
    this.desiredX = 0;
    this.desiredY = 0;
    this.path = [];
    this.graph = new Graph(OurGame.booleanMap);
    this.desiredPathChanged = false;
}
MultiPlayer.inherits(Player);

MultiPlayer.method('setPos', function (x_abs, y_abs) {      //override!
    this.x = x_abs * OurGame.grid;
    this.y = y_abs * OurGame.grid;
    this.desiredX = this.x;
    this.desiredY = this.y;
});
MultiPlayer.method('setDesired', function (x, y) {
//    log("SET DESIRED x: " + x + " y: " + y);
    this.desiredX = Number(x);
    this.desiredY = Number(y);
    this.desiredPathChanged = true;
});

MultiPlayer.method('getFollowingPath', function () {
    if (OurGame.mapChanged) {
//        log("NEW GRAPH!");
        this.graph = new Graph(mapToBoolean());
        OurGame.mapChanged = false;
    }

    if (!this.isMoving()) { //if full step has been made
        var start = this.graph.nodes[Math.floor(this.x / OurGame.grid)][Math.floor(this.y / OurGame.grid)];
        var end = this.graph.nodes[Math.floor(this.desiredX)][Math.floor(this.desiredY)];
//            log("START: " + start);
//            log("END: " + end);
//            log("GRAPH: " + this.graph)
        this.path = astar.search(this.graph.nodes, start, end);
//        log("PATH: " + this.path);
        this.desiredPathChanged = false;
    }else {
//         log("STILL MOVING 2!");
    }


});
MultiPlayer.method('update', function () {
    var change = this.uber('update'); // call the inherited Player update!
//    log("posX: " + this.getPosX() + " posY: " + this.getPosY() + " DESIRED x: " + this.desiredX + " y: " + this.desiredY);
    if (this.desiredPathChanged) {
//        log("NEW PATH!");
        this.getFollowingPath();
        this.x_move = 0;
        this.y_move = 0;
    }
    this.go();
    return change;
});
MultiPlayer.method('go', function () {
    var nextStep = this.path[0];
    if (nextStep) {
        if (!this.isMoving()) {
            this.x_move = (nextStep.x - (this.x / OurGame.grid)) * OurGame.grid;
            this.y_move = (nextStep.y - (this.y / OurGame.grid)) * OurGame.grid;
//            log("x_move: " + this.x_move + " y_move: " + this.y_move);
            this.path.shift(); //remove done step
        } else {
//            log("STILL MOVING!");
        }
    }
});